package com.library.servlet.student;

import com.library.entity.Student;
import com.library.service.student.LibcardService;
import com.library.servlet.BaseServlet;
import com.library.util.StudentContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;

@WebServlet("/student/libcard/*")
public class LibcardServlet extends BaseServlet {
    private LibcardService libcardService = new LibcardService();

    // 查询借书证
    protected void showLibCard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = StudentContext.getStudent();
        String sno = student.getSno();
        req.setAttribute("libCard", libcardService.getLibCard(sno));
        req.getRequestDispatcher("/WEB-INF/student/showLibCard.jsp").forward(req, resp);
    }
    // 新办借书证
    protected void applyLibCard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = StudentContext.getStudent();
        String sno = student.getSno();
        String cardNo = "LC" + sno;
        String sname = req.getParameter("sname");
        String type = req.getParameter("type");
        String collage = req.getParameter("collage");
        String major = req.getParameter("major");
        String birthStr = req.getParameter("birth");
        String originPlace = req.getParameter("originPlace");
        Date birth = null;
        if (birthStr != null && !birthStr.isEmpty()) {
            birth = Date.valueOf(birthStr);
        }
        String resultMsg = libcardService.applyLibCard(sno, cardNo, sname, type, collage, major, birth, originPlace);
        req.setAttribute("resultMsg", resultMsg);
        req.getRequestDispatcher("/WEB-INF/student/borrowResult.jsp").forward(req, resp);
    }
    // 挂失借书证
    protected void reportLossLibCard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = StudentContext.getStudent();
        String sno = student.getSno();
        String resultMsg = libcardService.reportLossLibCard(sno);
        req.setAttribute("resultMsg", resultMsg);
        req.getRequestDispatcher("/WEB-INF/student/borrowResult.jsp").forward(req, resp);
    }
    // 注销借书证
    protected void cancelLibCard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = StudentContext.getStudent();
        String sno = student.getSno();
        String resultMsg = libcardService.cancelLibCard(sno);
        req.setAttribute("resultMsg", resultMsg);
        req.getRequestDispatcher("/WEB-INF/student/borrowResult.jsp").forward(req, resp);
    }
    // 补办借书证
    protected void reissueLibCard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = StudentContext.getStudent();
        String sno = student.getSno();
        String resultMsg = libcardService.reissueLibCard(sno);
        req.setAttribute("resultMsg", resultMsg);
        req.getRequestDispatcher("/WEB-INF/student/borrowResult.jsp").forward(req, resp);
    }
} 